package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthMp;
import com.elsa.DataStatistics.pojo.YearMp;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class YearMPStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private YearMpMapper yearMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void yearMPStatistics(String oId) {

        int yId = timeUtil.getYearTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            YearMp yearMp = yearMpMapper.selectYearMpByDIdAndMIdAndCId(yId, menuOrderList.get(i).getCId(), menuOrderList.get(i).getMId());
            if(yearMp == null) {
                yearMp = new YearMp();
                yearMp.setYmpId(yearMpMapper.selectCount(null) + 1);
                yearMp.setYpId(yId);
                yearMp.setMId(menuOrderList.get(i).getMId());
                yearMp.setMName(menuOrderList.get(i).getMName());
                yearMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearMp.setYmpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                yearMp.setYmpNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(yearMp));
                yearMpMapper.insert(yearMp);
            } else {
                yearMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearMp.setYmpCalorie(yearMp.getYmpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                yearMp.setYmpNum(yearMp.getYmpNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(yearMp));
                yearMpMapper.updateById(yearMp);
            }
        }
    }

}
